//****************************************************************
//* Copyright (c) 2008 Ford Motor Company. All Rights Reserved.
//*
//* $$Workfile:   CostStructureFacade.java  $$
//* $$Revision:   1.2  $$
//* $$Author:   TMALDON4  $$
//* $$Date:   Feb 26 2009 07:30:20  $$
//*
//*****************************************************************
package ar.com.ford.it.fullEconomics.facade;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.List;
import ar.com.ford.it.fullEconomics.exception.FullEconomicsException;
import ar.com.ford.it.fullEconomics.logic.CostStructureLogic;
import ar.com.ford.it.fullEconomics.logic.ItemCategoryLogic;
import ar.com.ford.it.fullEconomics.logic.ItemLogic;
import ar.com.ford.it.fullEconomics.persistence.dto.Teesc02Coststvers;
import ar.com.ford.it.fullEconomics.persistence.dto.Teesc03Subcoststr;
import ar.com.ford.it.fullEconomics.persistence.dto.Teesc05Itemcatgry;
import ar.com.ford.it.fullEconomics.persistence.dto.Teesc06Item;
import ar.com.ford.it.fullEconomics.persistence.dto.Teesc07Subcstitem;
import ar.com.ford.it.fullEconomics.persistence.dto.Teesg01Plant;
import ar.com.ford.it.fullEconomics.struct.CostStructureCreateStruct;
import ar.com.ford.it.fullEconomics.struct.CostStructureFindStruct;
import ar.com.ford.it.fullEconomics.struct.OptionCombo;
import ar.com.ford.it.fullEconomics.wslx.FullEconomicsWslxUser;

public class CostStructureFacade {

    /**
     * Busca Item Categories segun el code o name para la planta pasada por
     * parametro
     * 
     * @param code
     * @param name
     * @param plant
     * @return
     * @throws FullEconomicsException
     */
    public Collection<Teesc05Itemcatgry> findItemCategories(String code,
                                                            String name,
                                                            Teesg01Plant plant)
            throws FullEconomicsException {
        Collection<Teesc05Itemcatgry> res = null;
        ItemCategoryLogic logic = new ItemCategoryLogic();

        res = logic.findItemCategories(code, name, plant);
        return res;
    }

    /**
     * Busca todos los Item Cateogories pertenecientes a "plant"
     * 
     * @param plant
     * @return
     * @throws FullEconomicsException
     */
    public Collection<Teesc05Itemcatgry> findAllItemCategories(
            Teesg01Plant plant)
            throws FullEconomicsException {
    	Collection<Teesc05Itemcatgry> res = null;
    	ItemCategoryLogic logic = new ItemCategoryLogic();

    	res = logic.findAllItemCategories(plant);
    	List lst = new ArrayList(res); 

    	Collections.sort(lst, new java.util.Comparator () {
    		public int compare(Object o1, Object o2) {
    			
    			Teesc05Itemcatgry item1 = (Teesc05Itemcatgry) o1;
    			Teesc05Itemcatgry item2 = (Teesc05Itemcatgry) o2;
    			String nombre1 = item1.getEesc05ItemcatgryNameN();
    			String nombre2 = item2.getEesc05ItemcatgryNameN();
    			return nombre1.compareTo(nombre2);
    		}

    	});

return lst;
}

    /**
     * Da de alta el Item Category pasado por parametro
     * 
     * @param itemCategory
     * Item Category a dar de alta
     * @param user
     * Usuario que da de alta el Item Category
     * @throws FullEconomicsException
     */
    public void createItemCategory(Teesc05Itemcatgry itemCategory,
                                   FullEconomicsWslxUser user)
            throws FullEconomicsException {
        ItemCategoryLogic logic = new ItemCategoryLogic();

        logic.createItemCategory(itemCategory, user);
    }

    /**
     * Actualiza el Item Category pasado por parametro
     * 
     * @param itemCategory
     * Item Category a actualizar
     * @param user
     * Usuario que actualiza el Item Category
     * @throws FullEconomicsException
     */
    public void updateItemCategory(Teesc05Itemcatgry itemCategory,
                                   FullEconomicsWslxUser user)
            throws FullEconomicsException {
        ItemCategoryLogic logic = new ItemCategoryLogic();

        logic.updateItemCategory(itemCategory, user);
    }

    /**
     * Elimina el Item Category pasado por parametro
     * 
     * @param itemCategory
     * Item Category a eliminar
     * @throws FullEconomicsException
     */
    public void deleteItemCategory(Teesc05Itemcatgry itemCategory)
            throws FullEconomicsException {
        ItemCategoryLogic logic = new ItemCategoryLogic();

        logic.deleteItemCategory(itemCategory);
    }

    /**
     * Se fija si ya existe un Item Category cuyo nombre sea "name"
     * 
     * @param name
     * Nombre del Item Category que quiero ver si existe
     * @param plant
     * Planta para la cual se verifica si hay Item Categories repetidos
     * @return True si ya existe un Item Category cuyo nombre es "name". False
     * en caso contrario
     * @throws FullEconomicsException
     */
    public boolean itemCategoryNameAlreadyExists(String name, Teesg01Plant plant)
            throws FullEconomicsException {
        ItemCategoryLogic logic = new ItemCategoryLogic();
        boolean ret = logic.itemCategoryNameAlreadyExists(name, plant);

        return ret;
    }

    /**
     * 
     * @param itemCategoryCode
     * @param itemCode
     * @param itemName
     * @param plant
     * @return
     * @throws FullEconomicsException
     */
    public Collection<Teesc06Item> findItems(String itemCategoryNumber,
                                             String itemNumber,
                                             String itemName, Teesg01Plant plant)
            throws FullEconomicsException {
        ItemLogic logic = new ItemLogic();
        Collection<Teesc06Item> res = null;

        res = logic.findItems(itemCategoryNumber, itemNumber, itemName, plant);
        return res;
    }

    /**
     * Crea el Item pasado por parametro
     * 
     * @param item
     * Item con el "name" seteado
     * @param itemCatNumber
     * Number del Item Category que se le va a asignar al Item creado
     * @param economicNumber
     * Number del Economic que se le va a asignar al Item creado
     * @param user
     * Usuario que crea el Item
     * @throws FullEconomicsException
     */
    public void createItem(Teesc06Item item, String itemCatNumber,
                           String economicNumber, FullEconomicsWslxUser user)
            throws FullEconomicsException {
        ItemLogic logic = new ItemLogic();

        logic.createItem(item, itemCatNumber, economicNumber, user);
    }

    /**
     * Actualiza el Item pasado por parametro
     * 
     * @param item
     * Item que contiene el nuevo Name
     * @param itemCatNumber
     * Number del Item Category que se le va a asignar al Item
     * @param economicNumber
     * Number del Economic que se le va a asignar al Item
     * @param user
     * Usuario que actualiza el Item
     * @throws FullEconomicsException
     */
    public void updateItem(Teesc06Item item, String itemCatNumber,
                           String economicNumber, FullEconomicsWslxUser user)
            throws FullEconomicsException {
        ItemLogic logic = new ItemLogic();

        logic.updateItem(item, itemCatNumber, economicNumber, user);
    }

    /**
     * Eliminar el Item pasado por parametro
     * 
     * @param item
     * Item a eliminar
     * @throws FullEconomicsException
     */
    public void deleteItem(Teesc06Item item) throws FullEconomicsException {
        ItemLogic logic = new ItemLogic();

        logic.deleteItem(item);
    }

    /**
     * Determina si el Item pasado por parametro puede ser propuesto
     * 
     * @param item
     * Item a determinar si puede ser propuesto
     */
    public boolean itemCanBeProposed(Teesc06Item item) {
        ItemLogic logic = new ItemLogic();
        boolean ret = false;

        ret = logic.itemCanBeProposed(item);
        return ret;
    }

    /**
     * Determina si el item pasado por parametro puede ser aprobado
     * 
     * @param item
     * Item a determinar si puede ser aprobado
     * @param user
     * @return
     */
    public boolean itemCanBeApproved(Teesc06Item item,
                                     FullEconomicsWslxUser user) {
        ItemLogic logic = new ItemLogic();
        boolean ret = false;

        ret = logic.itemCanBeApproved(item, user);
        return ret;
    }

    /**
     * Propone el Item pasado por parametro
     * 
     * @param item
     * Item a proponer
     * @throws FullEconomicsException
     */
    public void proposeItem(Teesc06Item item, FullEconomicsWslxUser user)
            throws FullEconomicsException {
        ItemLogic logic = new ItemLogic();

        logic.proposeItem(item, user);
    }

    /**
     * Aprueba el Item pasado por parametro
     * 
     * @param item
     * Item a aprobar
     * @throws FullEconomicsException
     */
    public void approveItem(Teesc06Item item, FullEconomicsWslxUser user)
            throws FullEconomicsException {
        ItemLogic logic = new ItemLogic();

        logic.approveItem(item, user);
    }

    /**
     * Determina si el Item Category pasado por parametro puede ser borrado.
     * Esto lo determina segun tenga o no Items haciendo referencia a el
     * 
     * @param itemCategory
     * @return True si puede ser borrado. False en caso contrario
     */
    public boolean itemCategoryCanBeDeleted(Teesc05Itemcatgry itemCategory) {
        ItemCategoryLogic logic = new ItemCategoryLogic();

        boolean ret = logic.itemCategoryCanBeDeleted(itemCategory);
        return ret;
    }

    /**
     * 
     * @return
     */
    public Collection<OptionCombo> getComboDateTypes() {
        CostStructureLogic logic = new CostStructureLogic();
        Collection<OptionCombo> ret = null;

        ret = logic.getComboDateTypes();
        return ret;
    }

    /**
     * 
     * @param parameters
     * @return
     * @throws FullEconomicsException
     */
    public Collection findCostStructureVersions(
                                                CostStructureFindStruct parameters)
            throws FullEconomicsException {
        CostStructureLogic logic = new CostStructureLogic();
        Collection ret = logic.findCostStructureVersions(parameters);

        return ret;
    }

    /**
     * 
     * @param subCostStructure
     * @param variableItems
     * @return
     * @throws FullEconomicsException
     */
    public Collection<Teesc07Subcstitem> findSubCostStructureItems(
                                                                   Teesc03Subcoststr subCostStructure,
                                                                   boolean variableItems)
            throws FullEconomicsException {
        Collection<Teesc07Subcstitem> ret = null;
        CostStructureLogic logic = new CostStructureLogic();

        ret = logic.findSubCostStructureItems(subCostStructure, variableItems);
        for (Teesc07Subcstitem subcstitem : ret) {
			if (subcstitem.getEvolveFromTemp()!=null)
				subcstitem.setEvolveFromTemp(null);
			if (subcstitem.getEvolveFromTempKey()!=null)
				subcstitem.setEvolveFromTempKey(null);
			if (subcstitem.getEvolveFromYearTemp()!=null)
				subcstitem.setEvolveFromYearTemp(null);
			if (subcstitem.getEvolveToTemp()!=null)
				subcstitem.setEvolveToTemp(null);
			if (subcstitem.getEvolveToTempKey()!=null)
				subcstitem.setEvolveToTempKey(null);
			if (subcstitem.getEvolveToYearTemp()!=null)
				subcstitem.setEvolveToYearTemp(null);
		}
        return ret;
    }

    public Teesc03Subcoststr getIncompleteSubCostStr(Teesc03Subcoststr father)
            throws FullEconomicsException {
        CostStructureLogic logic = new CostStructureLogic();
        Teesc03Subcoststr ret = null;
        ret = logic.getIncompleteSubCostStr(father);
        return ret;
    }

    /**
     * 
     * @param subCostStructure
     * @return
     * @throws FullEconomicsException
     */
    public Collection<Teesc03Subcoststr> findSubCostStructures(
                                                               Teesc03Subcoststr subCostStructure)
            throws FullEconomicsException {
        Collection<Teesc03Subcoststr> ret = null;
        CostStructureLogic logic = new CostStructureLogic();

        ret = logic.findSubCostStructures(subCostStructure);
        return ret;
    }

    /**
     * 
     * @param user
     * @param costStructureCreateStruct
     * @throws FullEconomicsException
     */
    public Teesc02Coststvers createCostStructure(
                                                 FullEconomicsWslxUser user,
                                                 CostStructureCreateStruct costStructureCreateStruct)
            throws FullEconomicsException {
        CostStructureLogic logic = new CostStructureLogic();

        return logic.createCostStructure(user, costStructureCreateStruct);
    }

    /**
     * 
     * @param user
     * @param subCostStr
     * @throws FullEconomicsException
     */
    public void updateSubCostStructure(FullEconomicsWslxUser user,
                                       Teesc03Subcoststr subCostStr)
            throws FullEconomicsException {
        CostStructureLogic logic = new CostStructureLogic();

        logic.updateSubCostStructure(user, subCostStr);
    }

    /**
     * 
     * @param user
     * @param costStrVersion
     * @param subCostStrFather
     * @param fatherTier
     * @param subCostStr
     * @throws FullEconomicsException
     */
    public void createSubCostStructure(FullEconomicsWslxUser user,
                                       Teesc02Coststvers costStrVersion,
                                       Teesc03Subcoststr subCostStrFather,
                                       int fatherTier,
                                       Teesc03Subcoststr subCostStr)
            throws FullEconomicsException {
        CostStructureLogic logic = new CostStructureLogic();

        logic.createSubCostStructure(user, costStrVersion, subCostStrFather,
                fatherTier, subCostStr);
    }

    /**
     * 
     * @param user
     * @param subCostStrItem
     * @param item
     * @param economicIndex
     * @throws FullEconomicsException
     */
    public Teesc07Subcstitem createSubCostStrItem(FullEconomicsWslxUser user,
                                     Teesc07Subcstitem subCostStrItem,
                                     String item, String economicIndex)
            throws FullEconomicsException {
        CostStructureLogic logic = new CostStructureLogic();

        logic.createSubCostStrItem(user, subCostStrItem, item, economicIndex);
        
        return subCostStrItem;
    }

    /**
     * 
     * @param user
     * @param subCostStrItem
     * @param item
     * @param economicIndex
     * @throws FullEconomicsException
     */
    public void updateSubCostStrItem(FullEconomicsWslxUser user,
                                     Teesc07Subcstitem subCostStrItem,
                                     String item, String economicIndex)
            throws FullEconomicsException {
        CostStructureLogic logic = new CostStructureLogic();

        logic.updateSubCostStrItem(user, subCostStrItem, item, economicIndex);
    }

    /**
     * 
     * @param plant
     * @param itemCategoryNumber
     * @return
     * @throws FullEconomicsException
     */
    public Collection<Teesc06Item> findApprovedItems(Teesg01Plant plant,
                                                     String itemCategoryNumber)
            throws FullEconomicsException {
        ItemLogic logic = new ItemLogic();
        Collection<Teesc06Item> res = null;

        res = logic.findApprovedItems(plant, itemCategoryNumber);
        return res;
    }

    /**
     * 
     * @param plant
     * @param itemNumber
     * @return
     * @throws FullEconomicsException
     */
    public String getEconomicIndexNumberForItem(Teesg01Plant plant,
                                                String itemNumber)
            throws FullEconomicsException {
        ItemLogic logic = new ItemLogic();
        String ret = null;

        ret = logic.getEconomicIndexNumberForItem(plant, itemNumber);
        return ret;
    }

    /**
     * 
     * @param subCostStrItems
     * @param itemNumber
     * @return
     */
    public boolean subCostStrItemAlreadyExists(
                                               Collection<Teesc07Subcstitem> subCostStrItems,
                                               String itemNumber) {
        CostStructureLogic logic = new CostStructureLogic();
        boolean ret = logic.subCostStrItemAlreadyExists(subCostStrItems,
                itemNumber);

        return ret;
    }

    /**
     * 
     * @param subCostStructures
     * @param subCsName
     * @return
     */
    public boolean subCostStrNameAlreadyExists(
                                               Collection<Teesc03Subcoststr> subCostStructures,
                                               String subCsName) {
        CostStructureLogic logic = new CostStructureLogic();
        boolean ret = logic.subCostStrNameAlreadyExists(subCostStructures,
                subCsName);

        return ret;
    }

    /**
     * 
     * @param subCostStrItem
     * @throws FullEconomicsException
     */
    public void deleteSubCostStrItem(Teesc07Subcstitem subCostStrItem)
            throws FullEconomicsException {
        CostStructureLogic logic = new CostStructureLogic();

        logic.deleteSubCostStrItem(subCostStrItem);
    }

    /**
     * 
     * @param subCostStr
     * @param costStrVersion
     * @throws FullEconomicsException
     */
    public void deleteSubCostStr(Teesc03Subcoststr subCostStr,
                                 Teesc02Coststvers costStrVersion)
            throws FullEconomicsException {
        CostStructureLogic logic = new CostStructureLogic();

        logic.deleteSubCostStr(subCostStr, costStrVersion);
    }

    /**
     * 
     * @param items
     * @param subCostStructures
     * @return
     */
    public BigDecimal getTotalSum(
                                  Collection<Teesc07Subcstitem> items,
                                  Collection<Teesc03Subcoststr> subCostStructures) {
        CostStructureLogic logic = new CostStructureLogic();
        BigDecimal total = null;

        total = logic.getTotalSum(items, subCostStructures);
        return total;
    }

    /**
     * 
     * @param user
     * @param version
     * @return
     * @throws FullEconomicsException
     */
    public Teesc02Coststvers createNewCostStructureVersion(
                                                           FullEconomicsWslxUser user,
                                                           Teesc02Coststvers version)
            throws FullEconomicsException {
        CostStructureLogic logic = new CostStructureLogic();

        return logic.createNewCostStructureVersion(user, version);
    }

    /**
     * 
     * @param user
     * @param version
     * @param csCreateStruct
     * @return
     * @throws FullEconomicsException
     */
    public Teesc02Coststvers copyCostStructureVersion(
                                                      FullEconomicsWslxUser user,
                                                      Teesc02Coststvers version,
                                                      CostStructureCreateStruct csCreateStruct)
            throws FullEconomicsException {
        CostStructureLogic logic = new CostStructureLogic();

        return logic.copyCostStructureVersion(user, version, csCreateStruct);
    }

    /**
     * 
     * @param costStrVersion
     * @return
     */
    public boolean costStructureCanBeProposed(Teesc02Coststvers costStrVersion) {
        CostStructureLogic logic = new CostStructureLogic();
        boolean ret = false;

        ret = logic.costStructureCanBeProposed(costStrVersion);
        return ret;
    }

    /**
     * 
     * @param costStrVersion
     * @param user
     * @throws FullEconomicsException
     */
    public void proposeCostStructure(Teesc02Coststvers costStrVersion,
                                     FullEconomicsWslxUser user)
            throws FullEconomicsException {
        CostStructureLogic logic = new CostStructureLogic();

        logic.proposeCostStructure(costStrVersion, user);
    }

    /**
     * 
     * @param costStrVersion
     * @param user
     * @return
     */
    public boolean costStructureCanBeApproved(Teesc02Coststvers costStrVersion,
                                              FullEconomicsWslxUser user) {
        CostStructureLogic logic = new CostStructureLogic();
        boolean ret = false;

        ret = logic.costStructureCanBeApproved(costStrVersion, user);
        return ret;
    }

    /**
     * 
     * @param costStrVersion
     * @param user
     * @throws FullEconomicsException
     */
    public void approveCostStructure(Teesc02Coststvers costStrVersion,
                                     FullEconomicsWslxUser user)
            throws FullEconomicsException {
        CostStructureLogic logic = new CostStructureLogic();

        logic.approveCostStructure(costStrVersion, user);
    }

    /**
     * 
     * @param user
     * @param costStrVersionForUpdate
     * @throws FullEconomicsException
     */
    public void updateCostStructureVersion(FullEconomicsWslxUser user,
                                           Teesc02Coststvers costStrVersion)
            throws FullEconomicsException {
        CostStructureLogic logic = new CostStructureLogic();

        logic.updateCostStructureVersion(user, costStrVersion);
    }

    /**
     * 
     * @param csVersionSelected
     * @throws FullEconomicsException
     */
    public void deleteSubCostStrVersion(FullEconomicsWslxUser user,
                                        Teesc02Coststvers costStrVersion)
            throws FullEconomicsException {
        CostStructureLogic logic = new CostStructureLogic();

        logic.deleteSubCostStrVersion(user, costStrVersion);
    }

    /**
     * 
     * @param costStrVersion
     * @return
     * @throws FullEconomicsException
     */
    public boolean checkIfallItemsAreApproved(Teesc02Coststvers costStrVersion)
            throws FullEconomicsException {
        boolean ret = false;
        CostStructureLogic logic = new CostStructureLogic();

        ret = logic.checkIfallItemsAreApproved(costStrVersion);
        return ret;
    }

}
